package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.Enum.ResultEnum;
import com.dongdongshop.core.SpecificPrint;
import com.dongdongshop.pojo.TbGoodsVo;
import com.dongdongshop.service.TbGoodsServiceI;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("tbGoosController")
public class TbGoosController {

    private Logger logger= LoggerFactory.getLogger(TbSellerController.class);

    @Reference
    private TbGoodsServiceI tbGoodsServiceI;


    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    //跳转查询页
    @RequestMapping("getFindAll")
    public String getFindAll(){
        return "admin/goods";
    }

    //跳转查询
    @RequestMapping("getGoods")
    @ResponseBody
    public SpecificPrint getGoods(String goodsName){
        try {
            List<TbGoodsVo> tbGoods = tbGoodsServiceI.getFindById(goodsName);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(tbGoods);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }

    //审核通过
    @RequestMapping("approvedGoods")
    @ResponseBody
    public SpecificPrint approvedGoods(String ids){
        try {
            //截取字符串
            String idss=ids.replaceAll("'","");
            String[] split = idss.split(",");
            //遍历出所有选中的ID
            List<Long> list=new ArrayList<>();
            for (String s : split) {
                list.add(Long.parseLong(s));
            }
            tbGoodsServiceI.UpdateBackgroundGoods(list);

            //mq发送
            rocketMQTemplate.convertAndSend("CCTV",ids);


            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",list);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            logger.info(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }

    @RequestMapping("rejectedGoods")
    @ResponseBody
    public SpecificPrint rejectedGoods(String ids){
        try {
            //截取字符串
            String idss=ids.replaceAll("'","");
            String[] split = idss.split(",");
            //遍历出所有选中的ID
            List<Long> list=new ArrayList<>();
            for (String s : split) {
                list.add(Long.parseLong(s));
            }
            tbGoodsServiceI.rejectedGoods(list);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",list);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            logger.info(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }

    //详情查询
    @RequestMapping("getFildAllId")
    @ResponseBody
    public SpecificPrint getFildAllId(Long id){
        try {
            Map<Object,Object>  map = tbGoodsServiceI.getFildAllId(id);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",map);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(map);
        }catch (Exception e){
            logger.info(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }
}
